<?php
class Content{
	private static $instance = NULL;
	public $extensions = array();
	
	
	public static function getInstance() {
		if(self::$instance === null) self::$instance = new Content;
		return self::$instance;	
	}
	
	private function __clone() {}
	private function __construct() {
		require_once './kernel/contentExtensionHandler/extensionInterface.php';
		require_once './kernel/contentExtensionHandler/DefaultHandler.php';
			
			
		$query = DB::getInstance()->query("SELECT file FROM ".Tables::$contentExtensionsTbl."");
		while($f = DB::getInstance()->fetchAssoc($query)) {
			require_once './kernel/contentExtensionHandler/'.$f['file'];
			$className = substr($f['file'],0,strrpos($f['file'],"."));
			$handler = new $className;
			if($handler instanceof ExtensionInterface) {
				foreach($handler->getHandledExtensions() as $handledExtension) {
					$this->extensions[$handledExtension] = $handler;
				} 
			}
		}
			
		// Load Extensions
		/*
		$DH = dir("./kernel/contentExtensionHandler/");
		while ($extFile = $DH->read()) {
	    	if (preg_match('/^..?$/', $extFile)) continue;
			if(is_dir("./kernel/contentExtensionHandler/".$extFile)) continue;
			if($extFile == "extensionInterface.php" || $extFile == "DefaultHandler.php") continue;
			
	    	require_once './kernel/contentExtensionHandler/'.$extFile;
			$className = substr($extFile,0,strrpos($extFile,"."));
			$handler = new $className;
			if($handler instanceof ExtensionInterface) {
				foreach($handler->getHandledExtensions() as $handledExtension) {
					$this->extensions[$handledExtension] = $handler;
				} 
			}
		}
	    $DH->close();
		*/
		// Add Default-Handler
		$handler = new DefaultHandler;
		$this->extensions['other'] = $handler;
	}
	
	
	public function getContent($file) {
		// Get Extension
		$extension = substr($file,strrpos($file,".")+1,strlen($file));
		
		if(array_key_exists($extension,$this->extensions)) {
			return $this->extensions[$extension]->handleExtension($file);
		}
		
		return $this->extensions['other']->handleExtension($file);
	}
	
	public static function outputFiles() {
		// Folder?
		$searchFolder = "";
		if(isset($_GET['folder'])) {
			General::getTpl()->assign('aktFolder',$_GET['folder']."/");
			$searchFolder = $_GET['folder']."/";
			
			//Pathstring
			$path = explode("/",trim($searchFolder,"/"));
			$outputPath = array();

			$outputPath["Root"] = "";
			foreach($path as $key=>$p) {
				$folderURL = "";
				$folder = "";
				for($i=0;$i<=$key;$i++) {
					$folderURL .= $path[$i]."/";
				}
				if($p != "") $outputPath[$p] = $folderURL;
			}

			General::getTpl()->assign('outputPath',$outputPath);
		}
		
		// Get Files
		$files = array();
		$folder = array();
		
		$DH = dir("./content/".$searchFolder);
		while ($extFile = $DH->read()) {
	    	if (preg_match('/^..?$/', $extFile)) continue;
			if(is_dir("./content/".$searchFolder.$extFile)) {
				// Folder
				$folder[] = $extFile;
			}else{
				$files[] = $extFile;
			}
		}
	    $DH->close();
		
		General::getTpl()->assign('files',$files);
		General::getTpl()->assign('folder',$folder);
		
		General::getTpl()->assign('smilieForm',
			General::getTpl()->fetch("popupContent.html"));
		
		General::getTpl()->display("popupIndex.html");
	}
}
?>